let express = require('express');
let crypto = require('crypto');
let router = express.Router();
let filter = require('../../components/filter');

/**
 * 查询主键数据 by p 2018-06-11
 *
 * 按条件查询主键数据
 *
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    req.app.db.query("select count(1) as totalCount from `keys` " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from `keys` " + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            res.json({"success": true, "data": results, "totalCount": totalCount, "message": ""});
        });
    });
});

/**
 * 查看主键数据 by p 2018-06-11
 *
 * 按id查看指定的主键信息
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from `keys` where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改主键数据 by p 2018-06-11
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;
    let order = 0;

    if (params.id === undefined || params.id === '') {
        res.json({"success": false, "message": "主键编码错误！"});
        return;
    }

    if (params.fatherId === undefined || params.fatherId === '' || params.fatherId < 0) {
        res.json({"success": false, "message": "父主键编码错误！"});
        return;
    }

    if (params.order !== undefined || params.order !== "") {
        order = params.order;
    }

    req.app.db.query("update `keys` set fatherId = ?, frontName = ?, frontLink = ?, backname = ?, backLink = ?, bigImage = ?, middleImage = ?, smallImage = ?, `order` = ? where id = ?", [params.fatherId, params.frontName, params.frontLink, params.backName, params.backLink, params.bigImage, params.middleImage, params.smallImage, order, params.id], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });

});

/**
 * 新增主键数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;
    let order = 0;

    if (params.name === undefined || params.name === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.values === undefined || params.values === "") {
        res.json({"success": false, "message": "值不能为空！"});
        return;
    }

    req.app.db.query("insert into `keys`(name, value) values(?, ?)" , [params.name, params.values], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});

/**
 * 删除主键信息 by v 2018-06-11
 *
 * 按id删除指定的主键信息
 */
router.delete('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from `keys` where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});

module.exports = router;